// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.skinnedmodel.animation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Quaternion;
import org.lwjgl.util.vector.Vector3f;
import zombie.core.PerformanceSettings;
import zombie.core.math.PZMath;
import zombie.core.skinnedmodel.HelperFunctions;

@Deprecated
public class StaticAnimation {
    private int framesPerSecond;
    public String name;
    public Matrix4f[][] Matrices;
    private Matrix4f[] RootMotion;
    public AnimationClip Clip;
    private int currentKeyframe = 0;
    private float currentTimeValue = 0.0F;
    private Keyframe[] Pose;
    private Keyframe[] PrevPose;
    private float lastTime = 0.0F;

    public StaticAnimation(AnimationClip clip) {
        this.Clip = clip;
        this.framesPerSecond = PerformanceSettings.BaseStaticAnimFramerate;
        this.Matrices = new Matrix4f[(int)((float)this.framesPerSecond * this.Clip.Duration)][60];
        this.RootMotion = new Matrix4f[(int)((float)this.framesPerSecond * this.Clip.Duration)];
        this.Pose = new Keyframe[60];
        this.PrevPose = new Keyframe[60];
        this.Create();
        Arrays.fill(this.Pose, null);
        this.Pose = null;
        Arrays.fill(this.PrevPose, null);
        this.PrevPose = null;
    }

    private Keyframe getNextKeyFrame(int int2, int int1, Keyframe keyframe2) {
        Keyframe[] keyframe0 = this.Clip.getKeyframes();

        for (int int0 = int1; int0 < keyframe0.length; int0++) {
            Keyframe keyframe1 = keyframe0[int0];
            if (keyframe1.Bone == int2 && keyframe1.Time > this.currentTimeValue && keyframe2 != keyframe1) {
                return keyframe1;
            }
        }

        return null;
    }

    public Quaternion getRotation(Quaternion quaternion, int _int) {
        if (this.PrevPose[_int] != null && PerformanceSettings.InterpolateAnims) {
            float _float = (this.currentTimeValue - this.PrevPose[_int].Time) / (this.Pose[_int].Time - this.PrevPose[_int].Time);
            if (this.Pose[_int].Time - this.PrevPose[_int].Time == 0.0F) {
                _float = 0.0F;
            }

            return PZMath.slerp(quaternion, this.PrevPose[_int].Rotation, this.Pose[_int].Rotation, _float);
        } else {
            quaternion.set(this.Pose[_int].Rotation);
            return quaternion;
        }
    }

    public Vector3f getPosition(Vector3f vector3f, int _int) {
        if (this.PrevPose[_int] != null && PerformanceSettings.InterpolateAnims) {
            float _float = (this.currentTimeValue - this.PrevPose[_int].Time) / (this.Pose[_int].Time - this.PrevPose[_int].Time);
            if (this.Pose[_int].Time - this.PrevPose[_int].Time == 0.0F) {
                _float = 0.0F;
            }

            PZMath.lerp(vector3f, this.PrevPose[_int].Position, this.Pose[_int].Position, _float);
            return vector3f;
        } else {
            vector3f.set(this.Pose[_int].Position);
            return vector3f;
        }
    }

    public void getPose() {
        Keyframe[] keyframe0 = this.Clip.getKeyframes();

        for (this.currentKeyframe = 0; this.currentKeyframe < keyframe0.length; this.currentKeyframe++) {
            Keyframe keyframe1 = keyframe0[this.currentKeyframe];
            if (this.currentKeyframe == keyframe0.length - 1 || !(keyframe1.Time <= this.currentTimeValue)) {
                if (PerformanceSettings.InterpolateAnims) {
                    for (int _int = 0; _int < 60; _int++) {
                        if (this.Pose[_int] == null || this.currentTimeValue >= this.Pose[_int].Time) {
                            Keyframe keyframe2 = this.getNextKeyFrame(_int, this.currentKeyframe, this.Pose[_int]);
                            if (keyframe2 != null) {
                                this.PrevPose[keyframe2.Bone] = this.Pose[keyframe2.Bone];
                                this.Pose[keyframe2.Bone] = keyframe2;
                            } else {
                                this.PrevPose[_int] = null;
                            }
                        }
                    }
                }
                break;
            }

            if (keyframe1.Bone >= 0) {
                this.Pose[keyframe1.Bone] = keyframe1;
            }

            this.lastTime = keyframe1.Time;
        }
    }

    public void Create() {
        float _float = (float)this.Matrices.length;
        double double0 = (double)this.Clip.Duration / (double)_float;
        double double1 = 0.0;
        int int0 = 0;

        for (Matrix4f matrix4f0 = new Matrix4f(); (float)int0 < _float; int0++) {
            this.currentTimeValue = (float)double1;
            this.getPose();

            for (int int1 = 0; int1 < 60; int1++) {
                if (this.Pose[int1] == null) {
                    this.Matrices[int0][int1] = matrix4f0;
                } else {
                    Quaternion quaternion = new Quaternion();
                    this.getRotation(quaternion, int1);
                    Vector3f vector3f = new Vector3f();
                    this.getPosition(vector3f, int1);
                    Matrix4f matrix4f1 = HelperFunctions.CreateFromQuaternionPositionScale(vector3f, quaternion, new Vector3f(1.0F, 1.0F, 1.0F), new Matrix4f());
                    this.Matrices[int0][int1] = matrix4f1;
                }
            }

            double1 += double0;
        }
    }

    public Keyframe interpolate(List<Keyframe> frames, float time) {
        int _int = 0;
        Keyframe keyframe0 = null;
        Object object = null;

        while (_int < frames.size()) {
            Keyframe keyframe1 = (Keyframe)frames.get(_int);
            if (keyframe1.Time > time && keyframe0.Time <= time) {
                Quaternion quaternion = new Quaternion();
                Vector3f vector3f = new Vector3f();
                float _float = (time - keyframe0.Time) / (keyframe1.Time - keyframe0.Time);
                PZMath.slerp(quaternion, keyframe0.Rotation, keyframe1.Rotation, _float);
                PZMath.lerp(vector3f, keyframe0.Position, keyframe1.Position, _float);
                Keyframe keyframe2 = new Keyframe();
                keyframe2.Position = vector3f;
                keyframe2.Rotation = quaternion;
                keyframe2.Scale = new Vector3f(1.0F, 1.0F, 1.0F);
                keyframe2.Time = keyframe0.Time + (keyframe1.Time - keyframe0.Time) * _float;
                return keyframe2;
            }

            _int++;
            keyframe0 = keyframe1;
        }

        return (Keyframe)frames.get(frames.size() - 1);
    }

    public void interpolate(List<Keyframe> frames) {
        if (!frames.isEmpty()) {
            if (!((Keyframe)frames.get(0)).Position.equals(((Keyframe)frames.get(frames.size() - 1)).Position)) {
                float _float = (float)(this.Matrices.length + 1);
                double double0 = (double)this.Clip.Duration / (double)_float;
                double double1 = 0.0;
                ArrayList arrayList = new ArrayList();

                for (int _int = 0; (float)_int < _float - 1.0F; double1 += double0) {
                    Keyframe keyframe = this.interpolate(frames, (float)double1);
                    arrayList.add(keyframe);
                    _int++;
                }

                frames.clear();
                frames.addAll(arrayList);
            }
        }
    }

    public void doRootMotion(List<Keyframe> frames) {
        float _float = (float)this.Matrices.length;
        if (frames.size() > 3) {
            for (int _int = 0; (float)_int < _float && _int < frames.size(); _int++) {
                Keyframe keyframe = (Keyframe)frames.get(_int);
                Quaternion quaternion = keyframe.Rotation;
                Vector3f vector3f = keyframe.Position;
                Matrix4f matrix4f = HelperFunctions.CreateFromQuaternionPositionScale(vector3f, quaternion, keyframe.Scale, new Matrix4f());
                this.RootMotion[_int] = matrix4f;
            }
        }
    }
}
